﻿/// <reference path="~/Scripts/FlowBasis/FlowBasis-all-vsdoc.js" />

FlowBasis.defineNamespace("Data");

FlowBasis.Data.ResultSetPager = function (options) {   
    var self = this;

    this.element = jQuery(options.container);

    this.resultSetList = options.resultSetList;

    this.hideIfOnlyOnePage = options.hideIfOnlyOnePage ? true : false;

    jQuery(this.resultSetList).bind("afterRender", function() {
        self.render();
    });
};

FlowBasis.Data.ResultSetPager.prototype.render = function () {
    var self = this;

    jQuery(document).ready(function () {
        self.element.html("");

        var pageInfo = self.resultSetList.getCurrentPageInfo();

        if (pageInfo != null)
        {
            if (self.resultSetList.hasPreviousPage()) {
                jQuery("<a>")
                    .attr("href", "javascript:;")
                    .text("prev")
                    .click(function () {
                        self.resultSetList.gotoPreviousPage();
                    })
                    .appendTo(self.element);
            }        

            self.element.append(" ");

            var hasNextPage = self.resultSetList.hasNextPage();
            if (hasNextPage) {
                jQuery("<a>")
                    .attr("href", "javascript:;")
                    .text("next")
                    .click(function () {
                        self.resultSetList.gotoNextPage();
                    })
                    .appendTo(self.element);
            }

            self.element.append(" ");

            var showPageAndResultCount = true;

            if (self.hideIfOnlyOnePage && pageInfo.pageCount == 1) {
                showPageAndResultCount = false;
            }

            if (showPageAndResultCount) {
                if (pageInfo.pageCount) {
                    self.element.append(" Page " + (pageInfo.pageIndex + 1) + " of " + pageInfo.pageCount);
                    self.element.append(", ");
                }        

                if (pageInfo.count > 0) {
                    self.element.append("Results " + (pageInfo.firstIndex + 1) + " to " + (pageInfo.firstIndex + pageInfo.count));
                    if (pageInfo.overallResultCount) {
                        self.element.append(" of " + pageInfo.overallResultCount);
                    }
                }
                else {
                    self.element.append("No Results");
                }
            }
        }
    });
};

